#!/usr/bin/env python
import sys
import signal

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

def onLoadFinished(result):
    	if not result:
        	print "Request failed"
        	sys.exit(1)

    	# Set the size of the (virtual) browser window
    	webpage.setViewportSize(webpage.mainFrame().contentsSize())

    	# Paint this frame into an image
    	image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
    	painter = QPainter(image)
    	webpage.mainFrame().render(painter)
    	painter.end()
    	image.save(sys.argv[2])
    	""" Launch signal to deamon
	    write URL to mysql """

app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)

webpage = QWebPage()
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.mainFrame().load(QUrl(sys.argv[1]))


sys.exit(app.exec_())
